package com.itheima.test21;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
题目二：
	用多线程的方式读取多个文件，要求：一个线程读取一个文件
 */
public class Test21 {
    public static void main(String[] args) {
        ArrayList<String> fileList = new ArrayList<>(List.of(
                "D:\\TestDir\\TestFile1.rar",
                "D:\\TestDir\\TestFile2.zip",
                "D:\\TestDir\\TestFile3.zip",
                "D:\\TestDir\\TestFile4.zip"
        ));

        for (String s : fileList) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        readFile(s);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public static void readFile(String s) throws IOException {
        long start = System.currentTimeMillis();
        System.out.println(s + "开始读取");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes))  != -1){

        }
        System.out.println(s + "读取完成");

        long end = System.currentTimeMillis();
        System.out.println(s + "读取用时" + (end - start) + "ms");
    }
}
